package com.it.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.domain.ResponseResult;
import com.it.domain.entity.Article;
import com.it.domain.entity.WebsiteInfo;
import com.it.domain.vo.WebsiteInfoVo;
import com.it.mapper.ArticleMapper;
import com.it.mapper.CategoryMapper;
import com.it.mapper.CommentMapper;
import com.it.mapper.WebsiteInfoMapper;
import com.it.service.WebsiteInfoService;
import com.it.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * (WebsiteInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-08-09 22:47:00
 */
@Service("websiteInfoService")
public class WebsiteInfoServiceImpl extends ServiceImpl<WebsiteInfoMapper, WebsiteInfo> implements WebsiteInfoService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private WebsiteInfoMapper websiteInfoMapper;
    @Override
    public ResponseResult selectWebsiteInfo() {
        WebsiteInfoVo websiteInfoVo = new WebsiteInfoVo();

        websiteInfoVo = BeanCopyUtils.copyBean(websiteInfoMapper.selectOne(null), WebsiteInfoVo.class);
        // 运行时长
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Article::getUpdateTime).orderByDesc(Article::getUpdateTime);
        websiteInfoVo.setArticleCount(Long.valueOf(articleMapper.selectCount(null)));
        List<String> listArticleContent = articleMapper.selectList(null).stream().map(Article::getContent).collect(Collectors.toList());
        // 合成一个string
        String mergedString = String.join("", listArticleContent);
        websiteInfoVo.setWordCount((long) extractTextFromMarkdown(mergedString).length());
        wrapper.clear();
        wrapper.select(Article::getViewCount);
        websiteInfoVo.setVisitCount(articleMapper.selectObjs(wrapper).stream().mapToLong(visitCount -> (Long) visitCount).sum());
        websiteInfoVo.setCategoryCount(Long.valueOf(categoryMapper.selectCount(null)));
        websiteInfoVo.setCommentCount(Long.valueOf(commentMapper.selectCount(null)));
        return ResponseResult.okResult(websiteInfoVo);
    }

    /**
     * 从Markdown文档中提取文字内容
     *
     * @param markdownContent Markdown文档内容
     * @return 文字内容
     */
    private static String extractTextFromMarkdown(String markdownContent) {
        // 使用正则表达式匹配Markdown文档中的文字内容，并去掉空格
        Pattern pattern = Pattern.compile("[^#>\\*\\[\\]`\\s]+");
        Matcher matcher = pattern.matcher(markdownContent);

        StringBuilder result = new StringBuilder();
        while (matcher.find()) {
            result.append(matcher.group()).append("\n");
        }

        return result.toString().trim();
    }
}

